/**
 * Created by Administrator on 2018/12/28 0028.
 */

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
//console.log(new Date(1234567897789).Format("yyyy年MM月dd日 hh:mm:ss"))

var util = {};

var createCheck = function(typ){
    return function(obj){
        return Object.prototype.toString.call(obj) == "[object "+ typ +"]";
    }
};

util.isNumber = createCheck("Number");
util.isString = createCheck("String");
util.isObject = createCheck("Object");
util.isArray = createCheck("Array");

util.isphone = function(num){
    console.log(num)
    var myreg = /^[1]{1}[345789]{1}[0-9]{9}$/;
    if (!myreg.test(num)) {
        return false;
    } else {
        return true;
    }
}

util.isNotEmptyObj = function(obj){
    if(obj && util.isObject(obj)){
        try{
            for(var i in obj){
                return true;
            }
            return false;
        }catch(e){
            return false;
        }
    }    
    return false;
}


util.throat = function(callback,num){
    var timer = null;
    callback = callback || function(){};
    return function(){
        if(!timer){
            var that = this;
            var arg = arguments;
            timer = setTimeout(function(){
                callback.apply(that,arg);
                timer = null;
            },num);
        }

    }
}


//将 textarea 输入的内容 换行 替换
util.replaceTextarea = function(text,p){
    if(text && util.isString(text)){
        return text.replace(/\r\n/g, p).replace(/\n/g, p).replace(/\s/g, ' ')
    }else{
        return '';
    }
}

//将textarea 输入的内容以换行分离为数组
util.splitTextarea = function(text){
    var list = [];
    if(text &&  util.isString(text)){
        text = util.replaceTextarea(text,"<br/>").split("<br/>");
        for(var i  = 0; i < text.length; i++){
            var str = text[i].trim();
            if(str !== ''){
                list.push(str);
            }
        }
        return list;
    }else{
        return list;
    }
}

util.map = function(param,cd){
    if(!param){
        return null;
    }
    cd = cd || function(){};
    var res = [];
    if(util.isArray(param) || util.isString(param)){
        for(var i = 0,len = param.length; i < len; i++){
            var r = cd(param[i],i);
            if(r === false){
                break;
            }else{
                res.push(r);
            }
        }
    }else if(util.isObject(param)){
        for(var i in param){
            var r = cd(param[i],i);
            if(r === false){
                break;
            }else{
                res.push(r);
            }
        }
    }else if(util.isNumber(param)){
        for(var i = 0; i < param; i++){
            var r = cd(i);
            if(r === false){
                break;
            }else{
                res.push(r);
            }
        }
    }else {
        return null;
    }
    return res;
}


export default util;